/**
  ******************************************************************************
  * File Name          : tskctrl.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"
#include "tskcfg.h"
#include <string.h>
#include "dclcd.h"
#include "gv.h"

/* task sync component */
TaskHandle_t xDeamon_Thd = NULL;
StaticTask_t xDeamon_Tcb;
StackType_t  xDeamon_Stk[DEAMON_STACK_SIZE];

void APP_CreateTask(void)
{
  app_queue_create();

  xStorage_Thd = xTaskCreateStatic(vStorage_Task, "para", STORAGE_STACK_SIZE, (void *)1, \
                                   STORAGE_TSK_PRIORITY, xStorage_Stk, &xStorage_Tcb);

  xDeamon_Thd = xTaskCreateStatic(vDeamon_Task, "ctrl", DEAMON_STACK_SIZE, (void *)1, \
                                  DEAMON_TSK_PRIORITY, xDeamon_Stk, &xDeamon_Tcb);

  xDcLcd_Thd = xTaskCreateStatic(vDcLcd_Task, "DcLc", DCLCD_STACK_SIZE, (void *)1, \
                                 DCLCD_TSK_PRIORITY, xDcLcd_Stk, &xDcLcd_Tcb);

  xIrKey_Thd = xTaskCreateStatic(vIrKey_Task, "irkey", IRKEY_STACK_SIZE, (void *)1, \
                                 IRKEY_TSK_PRIORITY, xIrKey_Stk, &xIrKey_Tcb);

  xRs485n1_Thd = xTaskCreateStatic(vRs485n1_Task, "4851", RS485N1_STACK_SIZE, (void *)1, \
                                   RS485N1_TSK_PRIORITY, xRs485n1_Stk, &xRs485n1_Tcb);

  xSensor_Thd = xTaskCreateStatic(vSensor_Task, "senso", SENSOR_STACK_SIZE, (void *)1, \
                                  SENSOR_TSK_PRIORITY, xSensor_Stk, &xSensor_Tcb);

//  xFiber_Thd = xTaskCreateStatic(vFiber_Task, "fiber", FIBER_STACK_SIZE, (void *)1, \
//                                 FIBER_TSK_PRIORITY, xFiber_Stk, &xFiber_Tcb);

//  xHlkb35_Thd = xTaskCreateStatic(vHlkb35_Task, "sens", HLKB35_STACK_SIZE, (void *)1, \
//                                  HLKB35_TSK_PRIORITY, xHlkb35_Stk, &xHlkb35_Tcb);
}

void APP_RunTimeCreateTask(void)
{
  xMeasureRt_Thd = xTaskCreateStatic(vMeasureRt_Task, "msrt", MEASURE_RT_STACK_SIZE, (void *)1, \
                                     MEASURE_RT_TSK_PRIORITY, xMeasureRt_Stk, &xMeasureRt_Tcb);
  
  xEthernet_Thd = xTaskCreateStatic(vEthernet_Task, "eth", ETHERNET_STACK_SIZE, (void *)1, \
                                    ETHERNET_TSK_PRIORITY, xEthernet_Stk, &xEthernet_Tcb);

  xModbusTCP_Thd = xTaskCreateStatic(vModbusTCP_Task, "mbtcp", MODBUSTCP_STACK_SIZE, (void *)1, \
                                     MODBUSTCP_TSK_PRIORITY, xModbusTCP_Stk, &xModbusTCP_Tcb);

  xProductionUP_Thd = xTaskCreateStatic(vProductionUP_Task, "proup", PRODUCTIONUP_STACK_SIZE, (void *)1, \
                                        PRODUCTIONUP_TSK_PRIORITY, xProductionUP_Stk, &xProductionUP_Tcb);

  xProductionRT_Thd = xTaskCreateStatic(vProductionRT_Task, "prort", PRODUCTIONRT_STACK_SIZE, (void *)1, \
                                        PRODUCTIONRT_TSK_PRIORITY, xProductionRT_Stk, &xProductionRT_Tcb);

  xProductionDB_Thd = xTaskCreateStatic(vProductionDB_Task, "prodb", PRODUCTIONDB_STACK_SIZE, (void *)1, \
                                        PRODUCTIONDB_TSK_PRIORITY, xProductionDB_Stk, &xProductionDB_Tcb);
}

void vDeamon_Task(void * pvParameters)
{
  TickType_t last_time;

  last_time = xTaskGetTickCount();

  while(1)
  {
    xTaskDelayUntil(&last_time, 500);

    dc_cmd_get_datetime();

    Led_toggle();
  }
}

/* GetIdleTaskMemory prototype (linked to static allocation support) */
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );

static StaticTask_t xIdleTaskTCBBuffer;
static StackType_t  xIdleStack[configMINIMAL_STACK_SIZE];

void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
{
  *ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer;
  *ppxIdleTaskStackBuffer = &xIdleStack[0];
  *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
}

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
